<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>table下拉选择器</title>
    <link media="all" rel="stylesheet" th:href="@{/libs/layui-v2.5.5/css/layui.css}">
    <link media="all" rel="stylesheet" th:href="@{/libs/font-awesome-4.7.0/css/font-awesome.min.css}">
    <link media="all" rel="stylesheet" th:href="@{/css/public.css}">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <form action="" class="layui-form" style="padding:20px;">
            <input name="id" th:value="${department.id}" type="hidden">
            <input id="parentId" name="parentId" th:value="${department.parentId}" type="hidden">
            <div class="layui-form-item required">
                <label class="layui-form-label">父节点</label>
                <div class="layui-input-inline">
                    <input autocomplete="off" class="layui-input" id="tree" name="" placeholder="请选择"
                           th:ts-selected="${department.parentId}"
                           th:value="${parentName}" type="text">
                </div>
            </div>
            <div class="layui-form-item required">
                <label class="layui-form-label required">菜单名</label>
                <div class="layui-input-block">
                    <input class="layui-input" lay-reqtext="菜单名不能为空" lay-verify="required" name="name"
                           placeholder="请输入菜单名" th:value="${department.name}">
                </div>
            </div>
            <div class="layui-form-item required">
                <label class="layui-form-label" for="">排序号</label>
                <div class="layui-input-block">
                    <input class="layui-input" lay-reqtext="排序号不能为空" lay-verify="required" name="idx"
                           placeholder="请输入排序号" th:value="${department.idx}" type="number">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">备注信息</label>
                <div class="layui-input-block">
                    <textarea class="layui-textarea" name="remark" placeholder="请输入备注信息"
                              th:text="${department.remark}"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-filter="saveBtn" lay-submit>确认保存</button>
                </div>
            </div>
        </form>
    </div>
</div>

<script charset="utf-8" th:src="@{/libs/layui-v2.5.5/layui.js}"></script>
<script charset="utf-8" th:src="@{/js/lay-config.js}"></script>
<script th:inline="none">
    layui.use(['table', 'form', 'tableSelect'], function () {
        var $ = layui.jquery,
            table = layui.table,
            form = layui.form,
            tableSelect = layui.tableSelect;

        tableSelect.render({
            elem: '#tree',
            checkedKey: 'id',
            height: '400',
            width: '900',
            searchType: 'more',
            searchList: [
                {searchKey: 'name', searchPlaceholder: '菜单名'},
            ],
            table: {
                response: {
                    statusCode: 200
                },
                url: '/system/depart/listPage',
                cols: [[
                    {type: 'radio'},
                    {field: 'id', title: 'ID'},
                    {field: 'name', minWidth: 200, title: '部门名'},
                    {field: 'idx', align: 'center', title: '排序号'},
                    {field: 'createBy', align: 'center', title: '创建人'},
                    {field: 'createTime', align: 'center', title: '创建时间'},
                    {field: 'remark', align: 'center', title: '备注'},
                ]]
            },
            done: function (elem, data) {
                var NEWJSON = []
                layui.each(data.data, function (index, item) {
                    NEWJSON.push(item.title)
                    $("#parentId").val(item.id)
                })
                elem.val(NEWJSON.join(","))
            }
        })

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var index = layer.alert("是否确认修改", {
                title: '最终的提交信息'
            }, function () {
                if ($('#isRoot').is(':checked') === true) {
                    data.field.parentId = -1
                }
                $.ajax({
                    type: "POST",
                    url: "/system/depart/update",
                    data: {
                        "id": data.field.id,
                        "name": data.field.name,
                        "idx": data.field.idx,
                        "remark": data.field.remark,
                        "parentId": data.field.parentId,
                    },
                    dataType: "json",
                    success: function (data) {
                        if (data.code === 200) {
                            // 关闭弹出层
                            layer.alert(data.message)
                            layer.close(index);
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                        } else {
                            layer.alert(data.message)
                        }
                    }
                });
            });
            return false;
        });
    });
</script>
</body>
</html>
